package com.yipinhouse.reception.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.yipinhouse.common.result.Result;
import com.yipinhouse.reception.client.UserWebManageClient;
import com.yipinhouse.reception.client.param.RegisterWebParam;
import com.yipinhouse.reception.client.param.UpdateInfoWebParam;
import com.yipinhouse.reception.common.utils.JwtUtil;
import com.yipinhouse.user.client.UserManageClient;
import com.yipinhouse.user.client.param.RegisterParam;
import com.yipinhouse.user.client.param.UpdateInfoParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("reception")
public class UserWebManageApiImpl implements UserWebManageClient {

    @Resource
    private UserManageClient userManageClient;

    /**
     * 用户注册
     *
     * @param registerParam
     * @return
     */
    @Override
    public Result register(RegisterWebParam registerParam) {
        log.info("用户注册：{}", registerParam);
        RegisterParam registerParams = BeanUtil.copyProperties(registerParam, RegisterParam.class);
        Result result = userManageClient.register(registerParams);
        return result;
    }

    /**
     * 短信验证码
     *
     * @param phone
     * @return
     */
    @Override
    public Result note(Long phone) {
        log.info("发送给手机号：{}", phone);
        Result result = userManageClient.note(phone);
        return result;
    }

    /**
     * 模拟短信验证码
     *
     * @return
     */
    @Override
    public Result noteTest(Long tel) {
        log.info("测试发送给手机号：{}", tel);
        Result result = userManageClient.noteTest(tel);
        return result;
    }

    /**
     * 修改个人信息与实名认证
     *
     * @param param
     * @return
     */
    @Override
    public Result updateInfo(UpdateInfoWebParam param) {
        UpdateInfoParam updateInfoParam = BeanUtil.copyProperties(param, UpdateInfoParam.class);
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String userId1 = request.getHeader("userId");
        Integer userId = Integer.valueOf(userId1);
        updateInfoParam.setUserId(userId);
        log.info("个人信息：{}", updateInfoParam);
        Result result = userManageClient.updateInfo(updateInfoParam);
        return result;
    }

//    /**
//     * 上传头像
//     *
//     * @param file
//     * @return
//     * @throws Exception
//     */
//    @Override
//    public Result upHeadphoto(MultipartFile file) throws Exception {
//        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = requestAttributes.getRequest();
//        String userId1 = request.getHeader("userId");
//        Integer userId = Integer.valueOf(userId1);
//        Result result = userManageClient.upHeadphoto(file, userId);
//        return result;
//    }
}
